var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "react", "services/page-datas", "controls/design-view", "services/local-service", "services/shopping", "components/product-info/component", "services/dataSources"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    const React = require("react");
    const page_datas_1 = require("services/page-datas");
    const design_view_1 = require("controls/design-view");
    const local_service_1 = require("services/local-service");
    const shopping_1 = require("services/shopping");
    const component_1 = require("components/product-info/component");
    const dataSources_1 = require("services/dataSources");
    class ProductPage extends React.Component {
        constructor(props) {
            super(props);
            this.state = { pageDataRecord: { pageData: page_datas_1.defaultPageDatas.empty() } };
            this.localService = this.props.createService(local_service_1.LocalService);
            this.shoppingService = this.props.createService(shopping_1.ShoppingService);
        }
        componentDidMount() {
            return __awaiter(this, void 0, void 0, function* () {
                let pageDataRecord = yield this.localService.getPageDataRecordById(this.props.data.id);
                console.assert(pageDataRecord != null);
                this.setState({ pageDataRecord });
                let designViewSave = this.designView.save;
                this.designView.save = () => __awaiter(this, void 0, void 0, function* () {
                    let productInfoData = page_datas_1.findChildComponentData(pageDataRecord, component_1.ProductInfo.name);
                    let product = productInfoData.props.product;
                    yield dataSources_1.dataSources.product.update(product);
                    return designViewSave.apply(this);
                });
            });
        }
        render() {
            let { pageDataRecord: pageData } = this.state;
            return React.createElement(design_view_1.DesignView, Object.assign({}, { app: this.props.app, }, { pageDataRecord: pageData, hidePageSettingPanel: true, ref: e => this.designView = this.designView || e }));
        }
    }
    exports.default = ProductPage;
});
